﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;

/// <summary>
/// 根据快捷键创建UGUI控件
/// 快捷键符号% Ctrl  # Shift & Alt   
/// </summary>
public class UGUIShortcutKey : Editor
{

    public const int UIlayer = 5;         //UI

    [MenuItem("Plateface/CreateUGUI Text #&L")]
    public static void CreateText()
    {
        if (Selection.gameObjects.Length > 0) 
        {
            GameObject obj = Selection.gameObjects[0];
            GameObject text = new GameObject();
            RectTransform textRect = text.AddComponent<RectTransform>();
            Text textTx = text.AddComponent<Text>();
            text.transform.SetParent(obj.transform);
            text.name = "Text";
            text.layer = UIlayer;
            textTx.text = "plateface";

            textRect.localScale = new Vector3(1, 1, 1);
            textRect.anchoredPosition = Vector2.zero;
            textRect.anchoredPosition3D = Vector3.zero;


            RectTransformZero(textRect);
        }


    }

    [MenuItem("Plateface/CreateUGUI Button #&B")]
    public static void CreateButton()
    {
        if (Selection.gameObjects.Length > 0)
        {
            Debug.Log("创建按钮");
            GameObject obj = Selection.gameObjects[0];
            if (obj == null) return;

            GameObject button = new GameObject();
            GameObject buttonTx = new GameObject();

            RectTransform buttonRect = button.AddComponent<RectTransform>();
            RectTransform buttonTxRect = buttonTx.AddComponent<RectTransform>();

            button.AddComponent<Image>();
            button.AddComponent<Button>();
            Text buttonTxTx = buttonTx.AddComponent<Text>();
            buttonTxTx.alignment = TextAnchor.MiddleCenter;
            

            button.transform.SetParent(obj.transform);
            buttonTx.transform.SetParent(button.transform);
            button.name = "Button";
            buttonTx.name = "Text";

            button.layer = UIlayer;
            buttonTx.layer = UIlayer;


            RectTransformZero(buttonRect);
            RectTransformZero(buttonTxRect);

        }
    }

    [MenuItem("Plateface/CreateUGUI Image #&S")]
    public static void CreateImage()
    {
        if (Selection.gameObjects.Length > 0)
        {
            Debug.Log("创建UGUI图片");
            GameObject obj = Selection.gameObjects[0];
            GameObject image = new GameObject();
            RectTransform imageRect = image.AddComponent<RectTransform>();
            image.AddComponent<Image>();
            image.transform.SetParent(obj.transform);
            image.name = "Image";
            image.layer = 5;

            RectTransformZero(imageRect);
        }

    }

    [MenuItem("Plateface/CreateUGUI InputField #&I")]
    public static void CreateInputField()
    {
        //创建UGUI标签
        GameObject obj = Selection.gameObjects[0];


        GameObject inputField = new GameObject();
        RectTransform rectTransform = inputField.AddComponent<RectTransform>();
        Image image = inputField.AddComponent<Image>();
        image.sprite = Resources.Load<Sprite>("UnityPlugins/UGUIShortcutKeyTexture/background1");
        inputField.AddComponent<InputField>();
        rectTransform.localScale = new Vector3(1, 1, 1);
        inputField.layer = UIlayer;

        inputField.transform.SetParent(obj.transform);
        inputField.name = "InputField";

        GameObject placeholder = new GameObject();
        Text placeholderTx = placeholder.AddComponent<Text>();
        placeholder.transform.SetParent(inputField.transform);
        placeholder.name = "Placeholder";
        placeholder.layer = UIlayer;
        placeholderTx.color = Color.black;

        GameObject text = new GameObject();
        Text textTx = text.AddComponent<Text>();
        text.transform.SetParent(inputField.transform);
        text.name = "Text";
        text.layer = UIlayer;

        textTx.color = Color.black;

        RectTransformZero(rectTransform);

    }

    public static void RectTransformZero(RectTransform rectTransform) 
    {
        rectTransform.localScale = new Vector3(1, 1, 1);
        rectTransform.anchoredPosition = Vector2.zero;
        rectTransform.anchoredPosition3D = Vector3.zero;
    }
}
